/*
 * Copyright 2009 BioTeam Inc
 * 
 * Licensed under License GNU LESSER GENERAL PUBLIC LICENSE version 3.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * 
 * http://www.gnu.org/copyleft/lesser.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package net.bioteam.appweb.gwt.apps.client.dataanalysis;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

import net.bioteam.appweb.gwt.apps.client.Util.UploadItemCallback;
import net.bioteam.appweb.gwt.apps.client.program.MyDataItem;
import net.bioteam.appweb.gwt.apps.client.program.ProgramForm;
import net.bioteam.appweb.model.Parameter;
import net.bioteam.appweb.model.Program;
import net.bioteam.appweb.model.meta.Entry;
import net.bioteam.appweb.submit.ProgramValueBean;

import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.Encoding;
import com.smartgwt.client.types.FormErrorOrientation;
import com.smartgwt.client.types.FormMethod;
import com.smartgwt.client.widgets.IButton;
import com.smartgwt.client.widgets.Label;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.events.DrawEvent;
import com.smartgwt.client.widgets.events.DrawHandler;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.FormItem;
import com.smartgwt.client.widgets.form.fields.HiddenItem;
import com.smartgwt.client.widgets.grid.events.CellClickEvent;
import com.smartgwt.client.widgets.grid.events.CellClickHandler;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.layout.VLayout;
import com.smartgwt.client.widgets.tree.Tree;
import com.smartgwt.client.widgets.tree.TreeGrid;
import com.smartgwt.client.widgets.tree.TreeGridField;
import com.smartgwt.client.widgets.tree.TreeNode;

public class DataInputPanel extends VLayout
{

	public static final int titleWidth=100;
	public DynamicForm inputForm;
	HiddenItem mobyType;
	Label messageLabel;
	public MyDataItem fileInput;
	public DataInputPanel()
	{
		this.setWidth100();
		this.setHeight100();
	
		inputForm=new DynamicForm();
		//inputForm.setBorder("2px solid red");
		inputForm.setEncoding(Encoding.MULTIPART);
		inputForm.setTarget("__gwt_upload");
		inputForm.setMethod(FormMethod.POST);
		inputForm.setTitleWidth(titleWidth);
		inputForm.setWidth(ProgramForm.CELL_WIDTH+titleWidth);
		inputForm.setCellBorder(5);
		inputForm.setHeight(200);
		
		//TODO
		inputForm.setAction("apps/user/datafileinput");
		inputForm.setShowInlineErrors(true);
		inputForm.setShowErrorText(true);
		inputForm.setErrorOrientation(FormErrorOrientation.TOP);
		inputForm.setRequiredTitleSuffix("<em style='color:red;'>***</em>");
		inputForm.setItemHoverWidth(300);
		
		List<FormItem> items=new LinkedList<FormItem>();
		
		Parameter param=new Parameter();
		param.setName("data");
		param.setPrompt("Input Data");
		param.setMandatory(true);
		param.setPrecond(null);
		
		Program fakeProgram=new Program();
		fakeProgram.setMetaData(new HashMap<String, Entry>());
		fakeProgram.get_Parameters().add(param);
		fakeProgram.IndexParameters();
		fakeProgram.getSimpleParameters().add(param);
		ProgramValueBean fakeBean=new ProgramValueBean(fakeProgram);
		
		
		fileInput=new MyDataItem(param, fakeBean, titleWidth);
		items.addAll(fileInput.getItems());
		fileInput.radioGroupItem.setCellStyle("dataradio1");
		
		mobyType=new HiddenItem("mobytype");
		items.add(mobyType);
		inputForm.setItems(items.toArray(new FormItem[0]));
		
		VLayout v=new VLayout();
		messageLabel=new Label();
		messageLabel.setAlign(Alignment.CENTER);
		messageLabel.setContents("Please select the data type");
		messageLabel.setBackgroundColor("yellow");
		
		v.addMember(inputForm);
		v.addMember(messageLabel);
		v.setHeight(400);
		v.setWidth(ProgramForm.CELL_WIDTH+60);
		
		
		final TreeGrid typeGrid = new TreeGrid();
		typeGrid.setWidth(300);
		typeGrid.setHeight(400);

		typeGrid.setShowRoot(false);
		// ----------edge

		//typeGrid.setShowEdges(true);
		//typeGrid.setBorder("opx");
		//typeGrid.setBodyStyleName("normal");

		typeGrid.setAnimateFolders(false);
		typeGrid.setShowHeader(false);
		
		TreeNode nodes[]=new TreeNode[1];
		TreeNode node=new TreeNode();
		node.setAttribute("name", "Object");
		node.setAttribute("description", "a base object class consisting of a namespace and an identifier");
		node.setAttribute("folder", true);
		nodes[0]=node;
		
		TreeGridField nameField=new TreeGridField("name", "Name");
		typeGrid.setFields(nameField);
		
		UsedTypeDataSource src=new UsedTypeDataSource();
		typeGrid.setDataSource(src);
		typeGrid.setLoadDataOnDemand(true);
		typeGrid.setInitialData(nodes);
		typeGrid.addDrawHandler(new DrawHandler()
		{
			public void onDraw(DrawEvent event)
			{
				Tree tree = typeGrid.getTree();
				//tree.getRoot().setAttribute("servername", "");
				//tree.setNameProperty("servername");
				tree.setPathDelim("/");
				tree.setIsFolderProperty("folder");
			}
		});
		typeGrid.addCellClickHandler(new CellClickHandler(){

			public void onCellClick(CellClickEvent event)
			{
				TreeNode treeNode = (TreeNode)event.getRecord();
				String name=treeNode.getAttribute("name");
				messageLabel.setContents(name+" is selected");
				mobyType.setValue(name);
			}});
		
		HLayout h=new HLayout();
		h.setWidth100();
		h.setHeight(410);
		h.addMember(v);
		h.addMember(typeGrid);
		
		HLayout bottom=new HLayout();
		bottom.setAlign(Alignment.RIGHT);
		
		bottom.setWidth(400);
		bottom.setHeight(30);
		
		Label label=new Label("(Step 1 of 3)");
		label.setHeight(20);
		label.setWidth(100);
		bottom.addMember(label);
		
		IButton button=new IButton("next->");
		bottom.addMember(button);
		button.addClickHandler(new ClickHandler(){

			public void onClick(ClickEvent event)
			{
				//History.newItem("user_analysis_2");
				if (inputForm.validate())
				{
					UploadItemCallback.setCallback();
					inputForm.submitForm();
				}
				
			}});
		
		addMember(bottom);
		addMember(h);
	}
	
	public void restoreDefaultMessage()
	{
		messageLabel.setContents("Please select the data type");
	}
}
